package com.shopmall.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.shopmall.goods.dao.GoodsRuleMapper;
import com.shopmall.goods.entity.GoodsRule;
import com.shopmall.goods.service.IGoodsRuleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class GoodsRuleServiceImpl extends ServiceImpl<GoodsRuleMapper, GoodsRule> implements IGoodsRuleService {

    @Override
    public IPage<GoodsRule> pageList(GoodsRule goodsRule) {
        IPage<GoodsRule> page = new Page<>(goodsRule.getPageNum(),goodsRule.getPageSize());
        LambdaQueryWrapper<GoodsRule> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(goodsRule.getRuleName())){
            wrapper.eq(GoodsRule::getRuleName,goodsRule.getRuleName());
        }
        IPage<GoodsRule> p = this.baseMapper.selectPage(page, wrapper);
        return p;
    }

    @Override
    public int addOrUpdate(GoodsRule goodsRule) {
        if (goodsRule.getId() == null) {

            return baseMapper.insert(goodsRule);
        } else {
            return baseMapper.updateById(goodsRule);
        }
    }

    @Override
    public int deleteById(Integer id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public List<GoodsRule> ruleList() {
        LambdaQueryWrapper<GoodsRule> wrapper = new LambdaQueryWrapper<>();
        return baseMapper.selectList(wrapper);
    }
}
